package com.boco.pciv.controller.project;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.boco.pciv.controller.PcivController;
import com.boco.pciv.controller.company.CompanyController;
import com.boco.pciv.core.entity.*;
import com.boco.pciv.entity.sale.ManageInfo;
import com.boco.pciv.entity.sale.ManagePayInfo;
import com.boco.pciv.service.sale.IManageCheckService;
import com.boco.pciv.service.sale.IManagePayCheckService;
import com.boco.pciv.service.sale.IManagePayService;
import com.boco.pciv.service.sale.IManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sunrise on 2020/7/16.
 */
@RestController
@RequestMapping(value="/managepay/")
public class ManagePayController extends PcivController {
    Logger logger = LoggerFactory.getLogger(CompanyController.class);

    @Resource
    private IManagePayService iManagePayService;
    @Resource
    private IManagePayCheckService iManagePayCheckService;
    @Resource
    private IManageCheckService iManageCheckService;
    @Resource
    private IManageService iManageService;
    /**
     * 列表
     */
    @GetMapping("/list")
    public PageResultBase list(String manageNo) {
        SearchCriteria param = new SearchCriteria();
        param.setIsPage(false);
        if (StrUtil.isNotBlank(manageNo)) {
            param.putCriteria("manageNo", manageNo);
        }
        PageResultBase result = iManagePayCheckService.getPage(param);
        return result;
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public BizResponse info(String  manageNo,String op) {
        CriteriaBase cond = new CriteriaBase();
        cond.putCriteria("manageNo", manageNo);
        if (StrUtil.isNotBlank(op)) {
            ManagePayInfo info = iManagePayService.getOne(cond);
            return BizResponseUtil.single(info);
        } else {
            ManagePayInfo info = iManagePayCheckService.getOne(cond);
            return BizResponseUtil.single(info);
        }
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    public BizResponse add(ManagePayInfo info) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("add data:{}", JSONUtil.toJsonStr(info));
            }
            if (info == null) {
                return BizResponseUtil.fail("新增信息不能为空");
            }
            if (info.getPayAmount() == null) {
                return BizResponseUtil.fail("拨付金额为空");
            }
            CriteriaBase cond = new CriteriaBase();
            cond.putCriteria("manageNo", info.getManageNo());
            ManageInfo manageInfo = iManageCheckService.getOne(cond);
            BigDecimal payAmount = info.getPayAmount().add(manageInfo.getPayAmount());
            if (manageInfo.getAmount().compareTo(payAmount) == -1) {
                return BizResponseUtil.fail("拨付金额 总和 大于 监管金额，填写错误");
            }
            manageInfo.setPayAmount(payAmount);
            manageInfo.setBalanceAmount(manageInfo.getAmount().subtract(payAmount));
            iManagePayCheckService.add(info);
            iManagePayService.add(info);

            iManageCheckService.edit(manageInfo);
            iManageService.edit(manageInfo);
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (Exception e) {
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR);
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public BizResponse delete(Integer id,String manageNo) {
        try {
            iManagePayCheckService.delete(id);
            SearchCriteria scond = new SearchCriteria();
            scond.setIsPage(false);
            scond.putCriteria("manageNo", manageNo);
            List<ManagePayInfo> list = iManagePayCheckService.getList(scond);
            BigDecimal payAmount = new BigDecimal(0);

            CriteriaBase base=new CriteriaBase();
            base.putCriteria("manageNo",manageNo);
            iManagePayService.delete(base);

            for (ManagePayInfo l : list) {
                payAmount = payAmount.add(l.getPayAmount());
                iManagePayService.add(l);
            }
            CriteriaBase cond = new CriteriaBase();
            cond.putCriteria("manageNo", manageNo);
            ManageInfo manageInfo = iManageCheckService.getOne(cond);
            manageInfo.setPayAmount(payAmount);
            manageInfo.setBalanceAmount(manageInfo.getAmount().subtract(payAmount));
            iManageCheckService.edit(manageInfo);
            iManageService.edit(manageInfo);
            return BizResponseUtil.success(MSG_ExeOK);
        } catch (Exception e) {
            logger.warn(MSG_ExeERROR, e);
            return BizResponseUtil.fail(MSG_ExeERROR);
        }
    }


}
